﻿//*************************************************************************************************
// SIMPLE ROGUE 08
// • CREATE A ROOM AND A HERO
// • ADD A SURROUNDING WALL
// • ADD RANDOM WALLS AND "+" TRAPS
// • Add Keyboard input to move up/down/left/right
// • Display messages when you hit a wall or trap
// • Change Walls to use extended characters (so it doesn't look like a "W").
// • Add Player Stats and display them at the bottom (also, messages are now in yellow) (Hero_Stats.cs)
// + Traps deal damage. Added Potions(!) to heal!
//*************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SimpleRogue
{
    public class SimpleRogue_08
    {
        // ****************************************************************************************
        // variables
        // ****************************************************************************************
        //Hero Stuff
        public static int HeroLocationX = 40;
        public static int HeroLocationY = 10;
        public static int HeroLocation_PreviousX = HeroLocationX;
        public static int HeroLocation_PreviousY = HeroLocationY;
        public static int HeroLocation_NextX = -1;
        public static int HeroLocation_NextY = -1;

        //Room Size
        public static int RoomWidth = 100;
        public static int RoomHeight = 50;
        //array to hold the game map and it's characters
        public static string[,] GameMap = new string[RoomWidth, RoomHeight];
        //wall
        public static string icon_Wall = ""; //This is set in the MAIN()
        //For displaying Messages
        public static string CurrentMessage = "";
        //To track how many times you hit a wall or trap
        public static int iHitWallCount = 0;
        public static int iHitTrapCount = 0;
        //Create new object, called MyHero to keep hero Stats
        public static Hero_Stats MyHero = new Hero_Stats();

        // ****************************************************************************************
        // MAIN (start)
        // ****************************************************************************************
        public static void Run_Rogue_08()
        {
            //Randomize Object
            Random objRandom = new Random();

            //-------------------------------------------------------------------------------------
            // Populate MyHero (stats) object with default values (randomize vlaues)
            //-------------------------------------------------------------------------------------
            MyHero.Name = "Turd Ferguson";
            MyHero.MaxLife = objRandom.Next(20, 40);
            MyHero.CurrentLife = MyHero.MaxLife;
            MyHero.Strength = objRandom.Next(10,21);
            MyHero.Dexterity = objRandom.Next(10,21);
            MyHero.Intelligence = objRandom.Next(10,21);
            MyHero.Wisdom = objRandom.Next(10,21);
            MyHero.Charisma = objRandom.Next(10,21);
            MyHero.Constitution = objRandom.Next(10,21);            

            //Set the encording to 1252 so we can use extended characters! Useful for making a wall look like a wall
            Console.OutputEncoding = System.Text.Encoding.GetEncoding(1252);

            //Wall will now look like a solid block
            icon_Wall = ((char)219).ToString(); ;



            //Hide Cursor
            Console.CursorVisible = false;

            //variable to capture keypresses
            ConsoleKeyInfo ckiKeyPressed;
            

            //-------------------------------------------------------------------------------------
            // Room size detials
            //-------------------------------------------------------------------------------------
            //Mapsize (Buffer)
            Console.BufferWidth = RoomWidth;
            Console.BufferHeight = RoomHeight;
            //Mapsize (Console Window)
            Console.WindowWidth = RoomWidth;
            Console.WindowHeight = RoomHeight;
            //Console.TreatControlCAsInput = true;            


            //-------------------------------------------------------------------------------------
            // Create one giant room -- values are placed in the array
            //-------------------------------------------------------------------------------------
            //Set all array elements to blank (ie, make each tile empty)
            for (int x = 0; x < RoomWidth - 0; x++)
            {
                for (int y = 0; y < RoomHeight - 0; y++)
                GameMap[x, y] = " ";                
            }
            //Create a Wall (Horizontal)
            for (int x = 2; x < RoomWidth - 2; x++)
            {
                GameMap[x, 2] = icon_Wall;
                GameMap[x, RoomHeight - 3] = icon_Wall;
            }
            //Create a Wall (Verticle)
            for (int y = 2; y < RoomHeight - 2; y++)
            {
                GameMap[2, y] = icon_Wall;
                GameMap[RoomWidth - 3, y] = icon_Wall;
            }
           
            //-------------------------------------------------------------------------------------
            // Add some random walls and + within the room
            //-------------------------------------------------------------------------------------
            for (int x = 0; x < 100; x++)
            {
                int RandomX = 0;
                int RandomY = 0;
                int RandomNumber = objRandom.Next(1,101);
                //Only add a wall/+ to a map location that the Hero is not standing in
                if (RandomX != HeroLocationX && RandomY != HeroLocationY)
                {
                    RandomX = objRandom.Next(3, RoomWidth - 3);
                    RandomY = objRandom.Next(3, RoomHeight - 3);
                    if (RandomNumber > 66)
                        GameMap[RandomX, RandomY] = "+"; //Add a Trap
                    else if (RandomNumber > 33)
                        GameMap[RandomX, RandomY] = "!"; //Add a Potion
                    else
                        GameMap[RandomX, RandomY] = icon_Wall;

                }
            }

            //-------------------------------------------------------------------------------------
            //DISPLAY MAP - aka write it out to the screen
            //-------------------------------------------------------------------------------------
            for (int x = 2; x < RoomWidth - 2; ++x)
            {
                for (int y = 2; y < RoomHeight - 2; ++y)
                {
                    Console.SetCursorPosition(x, y);
                    Console.Write(GameMap[x, y]);
                }
            }

            //---------------------------------------------------------------------------------
            // Display the Hero stats right away, before anything is done
            //---------------------------------------------------------------------------------
            Display_HeroStats();


            //-------------------------------------------------------------------------------------
            // Major Loop (loop the game)
            // This is where all the action takes place
            //-------------------------------------------------------------------------------------
            while (true)
            {

                //Draw Map Tile from PREVIOUS X/Y position
                Console.SetCursorPosition(HeroLocation_PreviousX, HeroLocation_PreviousY);
                Console.Write(GameMap[HeroLocation_PreviousX, HeroLocation_PreviousY]);

                //Draw Current HeroPosition, represented by an @ symbol (in yellow)
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.SetCursorPosition(HeroLocationX, HeroLocationY);
                Console.Write('@');
                Console.ForegroundColor = ConsoleColor.White;

                //---------------------------------------------------------------------------------
                // Keyboard Input - Read key entries
                //---------------------------------------------------------------------------------

                //Capture the key that the user is pressing
                ckiKeyPressed = Console.ReadKey(true);


                //Did user press an arrow button? If so, call the HeroMove() method
                switch (ckiKeyPressed.Key)
                {
                    case ConsoleKey.UpArrow:
                        HeroMove(0, -1);
                        break;
                    case ConsoleKey.DownArrow:
                        HeroMove(0, +1);
                        break;
                    case ConsoleKey.LeftArrow:
                        HeroMove(-1, 0);
                        break;
                    case ConsoleKey.RightArrow:
                        HeroMove(+1, 0);
                        break;
                }

                //---------------------------------------------------------------------------------
                // Display the Hero stats after any movement!
                //---------------------------------------------------------------------------------
                Display_HeroStats();

                //---------------------------------------------------------------------------------
                // Display Message
                // Always display the current message, even if it is blank. This method is found 
                // in the Message.cs class. Note that CurrentMessage is populated in 
                // the HeroMove() method
                //---------------------------------------------------------------------------------
                //Set Font color to Yellow
                Console.ForegroundColor = ConsoleColor.Yellow;            
                //Write Message
                Message.DisplayMessage(CurrentMessage);
                //Set Font back to yellow
                Console.ForegroundColor = ConsoleColor.White;            

            }                
        }

        // ****************************************************************************************
        // Hero Move()
        // ****************************************************************************************
        static void HeroMove(int x, int y)
        {

            //Determine next X/Y
            HeroLocation_NextX = HeroLocationX + x;
            HeroLocation_NextY = HeroLocationY + y;

            //-------------------------------------------------------------------------------------
            // Set the message / run into a wall or trap
            //-------------------------------------------------------------------------------------

            //Next location is a wall? If so, add a message then exit the method!
            if (GameMap[HeroLocation_NextX, HeroLocation_NextY] == icon_Wall)
            {
                iHitWallCount += 1;
                CurrentMessage = "DUDE... STOP HITTING THE WALL..." + " (" + iHitWallCount.ToString() + " times)";
                return;
            }   
                //It's a trap!
            else if (GameMap[HeroLocation_NextX, HeroLocation_NextY] == "+")
            {
                iHitTrapCount += 1;
                CurrentMessage = "YOU HIT A TRAP!" + " (" + iHitTrapCount.ToString() + " times)"; ;
                MyHero.CurrentLife -= 1; //Deal 1 damage to Hero
            }
                //It's a Healing Potion!
            else if (GameMap[HeroLocation_NextX, HeroLocation_NextY] == "!")
            {
                //Increase life?
                if (MyHero.CurrentLife == MyHero.MaxLife)
                {
                    CurrentMessage = "YOU ARE AT MAX LIFE!";
                }
                else
                { 
                    CurrentMessage = "Healed!";
                    MyHero.CurrentLife += 1;
                }
            }
                //It's just a blank space to move
            else
                CurrentMessage = "";           

            //-------------------------------------------------------------------------------------
            // Set the hero to the new location since it is legit
            //-------------------------------------------------------------------------------------
            //Set Previous X/Y
            HeroLocation_PreviousX = HeroLocationX;
            HeroLocation_PreviousY = HeroLocationY;
            //Set Current Hero Location to next location (i.e. move him)
            HeroLocationX = HeroLocation_NextX;
            HeroLocationY = HeroLocation_NextY;

        }

        // ****************************************************************************************
        // Hero Display_HeroStats()
        // ****************************************************************************************
        static void Display_HeroStats()
        {

            //Set Font color to Cyan first
            Console.ForegroundColor = ConsoleColor.Cyan;

            //-------------------------------------------------------------------------------------
            // Display all stats at the bottom
            //-------------------------------------------------------------------------------------
            //Set Cursor to 1,1
            Console.SetCursorPosition(2, RoomHeight -1);
            
            //Write Name, Hit Points. Example: JOHN DOE (30/30)

            Console.Write(MyHero.Name + " (" + MyHero.CurrentLife + " / " + MyHero.MaxLife + ")");

            //Set Cursor to 40,1
            Console.SetCursorPosition(40, RoomHeight - 1);

            //Display attributes
            Console.Write("STR: " + MyHero.Strength + " | ");
            Console.Write("DEX: " + MyHero.Dexterity + " | ");
            Console.Write("INT: " + MyHero.Intelligence + " | ");
            Console.Write("WIS: " + MyHero.Wisdom + " | ");
            Console.Write("CON: " + MyHero.Charisma + " | ");
            Console.Write("CHA: " + MyHero.Constitution);

            //Set color back to white
            Console.ForegroundColor = ConsoleColor.Cyan;            
        }

    }
}
